﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HtmlAgilityPack;
using System.Diagnostics;
using Windows.Phone.Speech.Synthesis;

namespace depl
{
    public partial class HtmlBox : UserControl
    {
        public event EventHandler HtmlLoaded;

        public string HtmlText
        {
            get { return (string)GetValue(HtmlTextProperty); }
            set { SetValue(HtmlTextProperty, value); }
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }


        public static readonly DependencyProperty HtmlTextProperty =
          DependencyProperty.Register(
              "HtmlText",                  // The name of the DependencyProperty
              typeof(string),             // The type of the DependencyProperty
              typeof(HtmlBox),       // The type of the owner of the DependencyProperty
              new PropertyMetadata(     // OnBlinkChanged will be called when Blink changes
                  "",                // The default value of the DependencyProperty
                  new PropertyChangedCallback(OnHtmlChanged)
              )
          );

        public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register(
          "Title",                  // The name of the DependencyProperty
          typeof(string),             // The type of the DependencyProperty
          typeof(HtmlBox),       // The type of the owner of the DependencyProperty
          new PropertyMetadata(     // OnBlinkChanged will be called when Blink changes
              ""
          )
        );

        private static void OnHtmlChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e
        )
        {

            var instance = d as HtmlBox;
            string htmlString = instance.HtmlText;
            instance.setHtml(htmlString);
        }

        public HtmlBox()
        {
            InitializeComponent();

        }

        public void setHtml(string htmlstring)
        {
            if (htmlstring == null)
                return;
            List<Block> blocks = new List<Block>();
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(htmlstring);
            Paragraph paragraph = new Paragraph();
            this.textContent.Blocks.Clear();
            foreach (HtmlNode node in doc.DocumentNode.ChildNodes)
            {
                switch (node.Name)
                {
                    case "b":
                        //Debug.WriteLine("bold: " + node.InnerText);
                        Bold bold = new Bold();
                        bold.Inlines.Add(node.InnerText);
                        paragraph.Inlines.Add(bold);
                        break;
                    case "a":
                        //Debug.WriteLine("hyperlink: " + node.InnerText);
                        Hyperlink hyperlink = new Hyperlink();
                        hyperlink.Inlines.Add(node.InnerText);
                        paragraph.Inlines.Add(hyperlink);
                        break;
                    case "i":
                        //Debug.WriteLine("italic: " + node.InnerText);
                        Italic italic = new Italic();
                        italic.Inlines.Add(node.InnerText);
                        paragraph.Inlines.Add(italic);
                        break;
                    case "font":
                        //Debug.WriteLine("color: " + node.InnerText);
                        Run run1 = new Run();
                        run1.Text = node.InnerText;
                        run1.Foreground = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);
                        paragraph.Inlines.Add(run1);
                        break;
                    default:
                        //Debug.WriteLine("normal: " + node.InnerText);
                        Run run2 = new Run();
                        run2.Text = node.InnerText;
                        paragraph.Inlines.Add(run2);
                        break;
                }

                //Debug.WriteLine("node name " + node.Name + " content: " + node.InnerText);

            }
            this.textContent.Blocks.Add(paragraph);

            if (this.HtmlLoaded != null)
                this.HtmlLoaded(this, null); 
        }

        private async void SpeakGerman(string text)
        {
            SpeechSynthesizer synth = new SpeechSynthesizer();
            IEnumerable<VoiceInformation> germanVoices = from voice in InstalledVoices.All
                                                         where voice.Language == "de-DE"
                                                         select voice;

            try
            {
                synth.SetVoice(germanVoices.ElementAt(0));
                await synth.SpeakTextAsync(text);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Debug.WriteLine("German voice not installed");
                MessageBox.Show(AppResources.LanguageNotInstalledText, AppResources.LanguageNotInstalledHeaderText, MessageBoxButton.OK);
            }
        }

        private void onDoubleTap(object sender, GestureEventArgs e)
        {
            if(Title!= null)
                SpeakGerman(Title);
        } 
    }
}
